package main

import (
	"fmt"
	"unsafe"
)

func main() {
	//baseDataTypeUsages()
	//intDetailUsages()
	//floatDetailUsages()
	//charDetailUsages()
	//stringDetailUsages()
	defaultValueOfBaseDataType()
}

func baseDataTypeUsages() {
	// int
	var i int = 1
	fmt.Println("i =", i)
	// int8
	var i8 int8 = 1
	fmt.Println("i8 =", i8)
	// int16
	var i16 int16 = 1
	fmt.Println("i16 =", i16)
	// int32
	var i32 int32 = 1
	fmt.Println("i32 =", i32)
	// int64
	var i64 int64 = 1
	fmt.Println("i64 =", i64)
	// uint
	var ui uint = 1
	fmt.Println("ui =", ui)
	// uint8
	var ui8 uint8 = 1
	fmt.Println("ui8 =", ui8)
	// uint16
	var ui16 uint16 = 1
	fmt.Println("ui16 =", ui16)
	// uint32
	var ui32 uint32 = 1
	fmt.Println("ui32 =", ui32)
	// uint64
	var ui64 uint64 = 1
	fmt.Println("ui64 =", ui64)
	// float32
	var f32 float32 = 1.0
	fmt.Println("f32 =", f32)
	// float64
	var f64 float64 = 2.222
	fmt.Println("f64 =", f64)
	// string
	var s string = "1123123132"
	fmt.Println("s =", s)
	// bool
	var b bool = true
	fmt.Println("b =", b)
	// 下面两个字符，直接输出的结果是字符对应的ascii码值
	// byte
	var b8 byte = 'A'
	fmt.Println("b8 =", b8)
	// rune
	var r rune = '阿'
	fmt.Println("r =", r)

}

func intDetailUsages() {
	var n1 int = 100
	fmt.Printf("type of n1 = %T\n", n1)

	// how to show type of data and bytes of data
	var n2 int64 = 100
	fmt.Printf("n2 = %d, type of n2 = %T, byte of n2 = %d\n",
		n2, n2, unsafe.Sizeof(n2))
}

func floatDetailUsages() {
	// 浮点数默认为float64
	var num = 0.0
	fmt.Printf("num = %f, type of num = %T\n", num, num)

	// 十进制数形式
	num2 := 1.1
	num3 := .123

	fmt.Println("num2 =", num2, "num3 =", num3)

	// 科学计数法
	num4 := 2.0e2
	num5 := 2.0e-2
	fmt.Println("num4 =", num4, "num5 =", num5)
}

func charDetailUsages() {
	// 使用fmt.Println输出字符，结果为对应的码值，想要输出字符，需要格式化输出
	var c1 rune = '阿'
	var c2 byte = '0'
	fmt.Println("c1 =", c1, "c2 =", c2)
	fmt.Printf("c1 = %c, c2 = %c\n", c1, c2)

	// 可以给变量赋值一个数字，格式化输出时会给出对应的字符
	var c3 = 38464
	fmt.Printf("c3 = %c\n", c3)

	// 字符类型可以进行计算
	var c4 byte = 'a'
	var c5 byte = 'b'
	var c6 byte = c4 + c5
	fmt.Println("c6 =", c6)
	fmt.Printf("c6 = %c\n", c6)
}

func stringDetailUsages() {
	// 字符串是不可变的
	var s1 string = "abc"
	// s1[0] = 'v'
	// Cannot assign to s1[0]
	fmt.Println("s1 =", s1)
	fmt.Println("--------------------------------")

	// 使用反引号会以字符串的原声形式输出，包括换行和特殊字符，可以实现防止攻击、输出源代码
	var s2 string = "abc\tabc"
	var s3 string = `abc\tabc`
	var s4 string = `
	package main
	
	import "fmt"
	
	func main() {
		fmt.Println("s1 =", s1)
	}
	`
	fmt.Println("s2 =", s2)
	fmt.Println("--------------------------------")
	fmt.Println("s3 =", s3)
	fmt.Println("--------------------------------")
	fmt.Println("s4 =\n", s4)

	// 字符串的拼接
	fmt.Println("--------------------------------")
	var s5 string = "hello" + " " + "world" + " " +
		"hello" +
		" " + "world"
	fmt.Println("s5 =", s5)
}

func defaultValueOfBaseDataType() {
	var i int
	var f float32
	var s string
	var b bool
	fmt.Println("i =", i, "f =", f, "s =", s, "b =", b)
	fmt.Printf("i = %v, f = %v, s = %v, b = %v", i, f, s, b)
}
